package cn.stylefeng.guns.modular.business.frequency.transportreserve.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.stylefeng.guns.core.exception.ServiceException;
import cn.stylefeng.guns.core.factory.PageFactory;
import cn.stylefeng.guns.core.pojo.page.PageResult;
import cn.stylefeng.guns.modular.business.frequency.transportreserve.entity.TransportReserve;
import cn.stylefeng.guns.modular.business.frequency.transportreserve.mapper.TransportReserveMapper;
import cn.stylefeng.guns.modular.business.frequency.transportreserve.model.param.TransportReserveParam;
import cn.stylefeng.guns.modular.business.frequency.transportreserve.service.TransportReserveService;
import cn.stylefeng.guns.modular.business.frequency.transportreserve.enums.TransportReserveExceptionEnum;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * 订单承运装车表 服务实现类
 *
 * @author yujie
 * @date 2023/05/09 17:15
 */
@Service
public class TransportReserveServiceImpl extends ServiceImpl<TransportReserveMapper, TransportReserve> implements TransportReserveService {

    @Override
    public PageResult<TransportReserve> page(TransportReserveParam transportReserveParam) {

        // 构造条件
        LambdaQueryWrapper<TransportReserve> queryWrapper = new LambdaQueryWrapper<>();

        // 查询分页结果
        return new PageResult<>(this.page(PageFactory.defaultPage(), queryWrapper));
    }

    @Override
    public List<TransportReserve> list(TransportReserveParam transportReserveParam) {

        // 构造条件
        LambdaQueryWrapper<TransportReserve> queryWrapper = new LambdaQueryWrapper<>();

        return this.list(queryWrapper);
    }

    @Override
    public void add(TransportReserveParam transportReserveParam) {

        // 将dto转为实体
        TransportReserve transportReserve = new TransportReserve();
        BeanUtil.copyProperties(transportReserveParam, transportReserve);

        this.save(transportReserve);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void delete(TransportReserveParam transportReserveParam) {
        this.removeById(transportReserveParam.getId());
    }

    @Override
    public void edit(TransportReserveParam transportReserveParam) {

        // 根据id查询实体
        TransportReserve transportReserve = this.queryTransportReserve(transportReserveParam);

        // 请求参数转化为实体
        BeanUtil.copyProperties(transportReserveParam, transportReserve);

        this.updateById(transportReserve);
    }

    @Override
    public TransportReserve detail(TransportReserveParam transportReserveParam) {
        return this.queryTransportReserve(transportReserveParam);
    }



    /**
     * 获取订单承运装车表
     *
     * @author yujie
     * @since 2023/05/09 17:15
     */
    private TransportReserve queryTransportReserve(TransportReserveParam transportReserveParam) {
        TransportReserve transportReserve = this.getById(transportReserveParam.getId());
        if (ObjectUtil.isEmpty(transportReserve)) {
            throw new ServiceException(TransportReserveExceptionEnum.NOT_EXISTED);
        }
        return transportReserve;
    }
}